package com.mini.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import org.apache.commons.lang3.tuple.Pair;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Objects;

/**
 * Created with meng.
 * User: meng5
 * Date: 2019-12-09
 * Description:
 */
public class BusinessUtil {

    public static void main(String[] args) throws ParseException {
//        String dateFormatExport = "yyyyMMdd";
//        String dateFormatOrder = "yyyy-MM-dd HH:mm";
//        String dateFormatDate = "yyyy-MM-dd HH:mm:ss";
//        String d = "2019-12-14 16:00";
//        Date startOrderDate = DateUtils.parseDate(d, dateFormatOrder);
//        LocalDate startLocalDate = startOrderDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
//        String start = DateFormatUtils.format(Date.from(startLocalDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()), dateFormatDate);
//        System.out.println(start);

        String[] phones = {"13921090938","15907847601","18200526456","15111651702","13860867673","15926281683","13565987078","18875217462","18166363870","18927212826","15180404913","15999980262","13528209553","13718414100","16601163573","15153728891","13794601691","13156282138","13522229636","15830901857","15152988867","13868686515","13617043813","18105614141","18280829537","13598214400","13631499623","18920505728","18553117795","13170029477","13923730521","13850808839","15096181810","13733819807","18890226219","13918733683","18082835736","18299895325","17850161262","13951104645","18303927700","13132086098","15285517479","13891787373","15250493146","15830817111","15254617723","15351235066","13958759814","13224153832","15897832654","13878252616","15891449337","13621321008","17752549055","13661884589","15274218762","15158936829","18338869399","15354905763","16697127654","15935250102","15831225528","18122860417","13723429425","15222417013","15678872677","13333328450","15035938244","18378222685","13896753105","13845811995","17691084669","13617509186","15916374232","15173623103","13735169096","15821968826","13717074899","13732282278","13189095972","15546135259","13759071065","18604595079","13547803676","15018490439","15618930110","15080046289","18295164356","13326875771","18610137898","18037169178","13736587509","13540354676","18299579917","18677637451","13952389260","18654695793","15968530152","13816488704","15991286263","13821770778","13530418230","13821770067","13306170568","13731863021","18128902162","13086720109","18761470566","13820365115","18659977775","18850739326","15609037111","18638890736","13007733305","13717790563","17799204886","13319941220","18186583058","18759130179","17785592324","15105509224","13158238939","18101691553","18337648790","18601296386","13503090030","15731250786","13942775533","15700087468","13817736380","13881754200","15057803303","18607871916","13409689270","17368457909","15562757072","13893104951","13086270488","18875404056","15203331259","15318660393","18080686446","15198741812","13062161666","13481199209","18268195157","13279977889","17369317902","13557819480","18873390698","15757363331","15632396509","15070837499","16697127654","13608051764","18591711880","18656497233","13513802389","15603639669","18759130179","15526650545","18747342236","18866893223","13733130167","13701193425","13343165782","15827239452","18351484789","18601211718","13453815462","13663562226","13066961669","18977489136","15088058362","18867701317","13924350520","18153380630","13600279862","15195000282","13658734830","13875326501","15175827008","13183159729","15275332350","15876531865","13162601237","15267083122","13934736699","13283224448","18337759936","13953852397","18379937033","13938435862","13937252214","18298478472","15900538661","18739938159","15838255855","13652041629","17388851682","15515655257","15804564209","17673627216","18513331349","15048799999","15868407586","18112868863","15154542311","14790779779","13267152409","18318251101","15805502261","18078417184","15170686101","15572487556","13806408700","15004165854","18237448822","13619790050","18394136370","15133997381","13209537061","13869622510","13132582015","13363302471","15801499078","15525913888","13610665334","13501060227","15881893331","15915850191","18991972182","18535562216","15559333871","13991984603","13589911806","13891725088","13392139648","15034627748","18918976827","15940030802","13542165984","18183748123","15334508070","18973733511","13833045441","13548718193","15179446299","17396744361","13431872121","18328584455","18097511306","18872867746","15215378025","13632413372","18679651706","18275911249","18503551186","13668546908","18720370527","13150212271","13600786589","18234108724","18078077747","15726669690","18268404813","15003210847","18071380759","13436061085","15093302064","15810564031","15589287358","13677088006","18346709845","15906887673","15178156555","19937977296","13588847332","13990818948","15013366136","17741162491","18222872954","18728971625","15855370267","13892729030","17865678493","13537758516","18562586236","13971716418","15062933177","13615606497","18097722283","18203466650","18615691356","13920223050","18213585704","18872720019","18600082113","13933222747","17725522661","13657598891","15860223829","13977971772","13602245855","15369334003","15256048834","13375622790","17282305230","13676088822","13595015094","19970757609","18958039389","17360480661","15829289667","13564280441","18610134467","15079665399","13901062072","18182625585","13540057964","17719188900","18246198765","15905566232","13937176989","13779388667","18062023420","13926598641","13984111419","15276622245","13528209553","18439611592","18801756421","13665607621","13457564813","15945550077","15718880603","18329532421","15247201765","18630521102","18855423621","18640860195","15812421354","15896066943","17752318879","18910017037","17799067236","13534482891","18205108308","13192826893","13795192757","13736610109","18625730915","17726919486","18931028248","18902176225","13810202430","15202930930","17601276299","15999980262","13359730333","15903880513","13676478850","15841228016","19951801359","13095075766","15283546214","13421378809","18007574672","18757543587","18386720224","15929038930","13230819437","13552303188","15620007695","15201385867","13309248703","18323377801","13667272115","13611388371","15267937737","15339331631","18379072942","13826547365","13551062691","13605263592","18951901380","15983048507","13942385559","17381945955","15918785473","18199610666","15256048834","15127711698","17719188900","15835560750","13921544778","13671869063","18067800266","18600827201","18737503125","18316294313","15369997077","17519512561","13975107132","15266995686","18707197125","15959230117","13631897294","13718414100","13809678560","15516489201","13728305186","18690270907","13196150382","13698607836","13824390362","15329112815","13552220032","15539446455","15280638491","13783475160","15300216514","13603110506","15846941995","15563083933","18213483409","18833221080","15909188355","13776819267","13711454589","13938218678","13593258891","16601163573","18999836015","13732033758","15920082261","15703106399","18962634513","18093638066","17515086716","13761232599","15375693020","13893839299","13311568780","18681727960","13570120210","13378258777","18210318366","18645657000","19999753068","19921057865","18235939753","18398943113","18782051580","13962092709","15725558234","13902037339","15913869486","13252739531","15590805220","15906335818","17702087653","19897494009","13856751087","17769013990","18290165315","15596007310","18169363850","15001306679","17648186832","15511784234","17325202846","15165319100","15855038925","13823860861","13599026501","15169553696","18351732129","13521430323","15152199895","13352213920","13507465826","13739770175","15103634465","18387760558","13872135016","18784730717","18605513695","15185229321","15599213337","18169449110","15987777807","13466984186","18632293963","13821101230","18221529064","18686843311","15277671865","18395120188","15206401760","18392656734","13662017445","17799940003","15998516663","13489986565","13614895976","18722935213","18327583998","18910530068","18574552797","15870282032","13940731032","18055077800","15519020777","18738388183","18883868805","13818586619","13120577776","13549773397","15999523867","13909900625","13670084753","15218784310","15873436588","15007550139","15195928597","13763604037","13530129487","15270622758","18181843560","13809689185","18851287538","13669457940","15613048966","13333975587","13971186410","13835673316","18056582013","15717385419","15701358705","13231602999","13363815602","13301295102","13939280302","13873409054","13668908163","18206093159","17307272510","18131830069","13944209589","13506901486","15383869352","15863386256","13863817117","15822540088","15010488928","13632630663","18855740517","13037179759","18219752192","18569071086","13681268718","18669081888","13916607003","15003339662","15106869057","15026308969","13891676266","13855084289","15999323016"};
        String str = "";
        for(String phone : phones){
            str += encrypt(phone) + "','";
        }
        System.out.println(str);
    }

    public static String encrypt(String phone){
        String key = "dqUTpKgLXEZzblMCmJTAqQuwyeWtpCja";
        AES aes = SecureUtil.aes(key.getBytes());
        String encode = aes.encryptHex(phone);
        return encode;
    }

    public static String decrypt(String phoneDecrypt){
        String key = "dqUTpKgLXEZzblMCmJTAqQuwyeWtpCja";
        AES aes = SecureUtil.aes(key.getBytes());
        String decode = aes.decryptStr(phoneDecrypt);
        return decode;

    }

    public static String durationFormat(Long sd){
        Long minute = 60L;
        if(sd == null){
            return null;
        }

        Long h = Math.floorDiv(sd, minute);
        Long m = Math.floorMod(sd, minute);

        return h +"分" + m + "秒";
    }
    /**
     * 对比，考虑null的情况
     * @param t1
     * @param t2
     * @param <T>
     * @return
     */
    public static <T extends Comparable> int compare(T t1, T t2){
        if(t1==null){
            if(t2 != null){
                return -1;
            }else{
                return 0;
            }
        }

        if(t2 == null){
            return 1;
        }

        return t1.compareTo(t2);

    }

    public static Pair<Date, Date> getOrderDuration(Date orderDate){
        if(orderDate == null){
            return null;
        }
        LocalDate orderLocalDate = orderDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        LocalDateTime orderLocalDateEnd = LocalDateTime.of(orderLocalDate, LocalTime.of(16, 0, 0));
        LocalDateTime orderLocalDateStart = orderLocalDateEnd.minusDays(1);
        Date start = Date.from(orderLocalDateStart.atZone(ZoneId.systemDefault()).toInstant());
        Date end = Date.from(orderLocalDateEnd.atZone(ZoneId.systemDefault()).toInstant());
        return Pair.of(start, end);
    }
    /**
     * 根据timePhase，转换为显示x~y的格式字符串
     * @param timePhase
     * @return
     */
    public static String convertTimePhaseShow(Integer timePhase) {
        if(timePhase == null){
            return null;
        }
        int start = (timePhase-1) * 10;
        int end = timePhase * 10;
        String show = String.valueOf(start) + "~" + String.valueOf(end);
        return show;
    }

    /**
     * 除法运算-返回Double
     * @param sub
     * @param all
     * @return
     */
    public static Double division(Number sub, Number all){
        if(sub == null){
            return null;
        }
        if(all == null || all.longValue() == 0){
            return null;
        }

        return sub.doubleValue() / all.doubleValue();
    }

    /**
     * 除法运算- 0.0x格式
     * @param sub
     * @param all
     * @return
     */
    public static String divisionResultNormal(Number sub, Number all){

        return division(sub, all, Format.DECIMAL_FORMAT_NORMAL);
    }

    public static String divisionResultNormal(Number d){

        return d == null ? null : Format.DECIMAL_FORMAT_NORMAL.format(d);
    }
    /**
     * 除法运算- 0.0x%
     * @param sub
     * @param all
     * @return
     */
    public static String divisionResultPercent(Number sub, Number all){
        return division(sub, all, Format.DECIMAL_FORMAT_PERCENT);
    }
    public static String divisionResultPercent(Double d){
        return d == null ? null : Format.DECIMAL_FORMAT_PERCENT.format(d);
    }

    /**
     * 除法运算-返回format格式的字符串
     * @param sub
     * @param all
     * @param format
     * @return
     */
    public static String division(Number sub, Number all, DecimalFormat format){
        Double r = division(sub, all);

        if(r == null){
            return null;
        }

        return format.format(r);

    }

    public static Integer add(Integer a,Integer b){
        if(Objects.isNull(a)){
            a=0;
        }
        if(Objects.isNull(b)){
            b=0;
        }
        return a+b;
    }

    public static Float add(Float a,Float b){
        if(Objects.isNull(a)){
            a=0F;
        }
        if(Objects.isNull(b)){
            b=0F;
        }
        return a+b;
    }


    public static class Format{
        /**
         * 保留两位小数
         */
        public static final DecimalFormat DECIMAL_FORMAT_NORMAL = new DecimalFormat("0.00");
        /**
         * 将浮点型数据转换为百分比，自动进行*100运算
         */
        public static final DecimalFormat DECIMAL_FORMAT_PERCENT = new DecimalFormat("0.00%");
        /**
         * 保留0位小数
         */
        public static final DecimalFormat DECIMAL_FORMAT_INTEGER = new DecimalFormat("0");

        final String DECIMAL_ZERO_PERCENT = "0.00%";
        final String DECIMAL_ZERO_NORMAL = "0.00";
        final String DECIMAL_NULL = "0";
    }
}